#include <wx/wx.h>
#include <wx/filename.h>
#include <wx/dir.h>

#include "thememgr.h"
#include "cfgmgr.h"

ThemeManager::ThemeManager() {
	wxLogMessage(_T("ThemeManager::ThemeManager"));
	
	themes_dir = ConfigManager::get().product_theme_dir.GetFullPath();
	
    _theme_dir_hash = ThemeDirHash(10);
    
	_bmp_hash = ThemeBmpHash(20);

    _init_theme_dirs();
    // _init_default_theme();
    _set_current_theme(_T("tangerine"));
}


void ThemeManager::_init_theme_dirs() {
    wxArrayString theme_dirs;
    wxDir themesdir(themes_dir.GetFullPath());
    ThemeDirsTraverser dir_trvser(theme_dirs);
    
    themesdir.Traverse(dir_trvser);
    size_t dir_count = theme_dirs.GetCount();
    
    for (size_t i=0;i<dir_count;i++) {
        const wxFileName& dir = wxFileName::DirName(theme_dirs[i]);

        // Get theme dir last name as Theme Name
        // ThemeName <=> ThemeDirFN
        _theme_dir_hash[dir.GetDirs()[dir.GetDirCount()-1]] = \
                    new wxFileName(theme_dirs[i],wxEmptyString);
        // 
        //wxPrintf(wxString::Format(_T("%d"),dir_count));
    }
}


void ThemeManager::_set_current_theme(const wxString& theme_name) {
    _current_theme_dir = *_theme_dir_hash[theme_name];
}


wxBitmap& ThemeManager::get_bitmap(const wxString& bmp_name){
    ThemeBmpHash::iterator it = _bmp_hash.find(bmp_name);
    
    if ( it==_bmp_hash.end()) {
        wxFileName bmp_file(_current_theme_dir);
        bmp_file.SetFullName(bmp_name);

        wxBitmap* bmp = new wxBitmap(bmp_file.GetFullPath(),wxBITMAP_TYPE_PNG);
        //wxMessageBox(bmp_file.GetFullPath());
        _bmp_hash[bmp_name] = bmp;

        return *bmp;
    }
    
    return *it->second;
}


/*wxBitmap& ThemeManager::get_bitmap(const wxString& bmp_rpath) {
	return 
}*/

ThemeManager* ThemeManager::_theme_mgr = NULL;
